home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 12 / Amiga Format AFCD12 (Apr 1997, Issue 96).iso / -in_the_mag- / html_tutorial / iostream.h < prev    next >
C/C++ Source or Header  |  1997-01-21  |  4KB  |  201 lines

  1. // Faked iostream.h to use less memory
  2.  
  3. #ifndef __IOSTREAM_H
  4. #define __IOSTREAM_H
  5.  
  6. #include <stdio.h>
  7. #undef putchar
  8. #undef getchar
  9.  
  10. inline int isdigit( char c )
  11. {
  12.   return (c>='0' && c<='9');
  13. }
  14.  
  15. class ios {
  16. public:
  17.   ios( FILE* );
  18.   ios( char name[], char mode[] );
  19.   enum Selector { dec, hex, oct, skipws, app };
  20.   void putchar( const char );
  21.   char getchar(void);
  22.   bool eof( void );
  23.   void ungetch( const char );
  24. private:
  25.   FILE *stream;          // Last character read
  26. };
  27.  
  28. ios::ios( FILE* f )
  29. {
  30.   stream = f;
  31. }
  32.  
  33. ios::ios( char name[], char mode[] )
  34. {
  35.   stream = fopen( name, mode );
  36. }
  37.  
  38. void ios::putchar( const char c )
  39. {
  40.   putc(c, stdout);
  41. }
  42.  
  43. char ios::getchar(void)
  44. {
  45.   return getc(stdin);
  46. }
  47.  
  48. void ios::ungetch(const char c)
  49. {
  50.   ::ungetc(c,stdin);
  51. }
  52.  
  53. bool ios::eof( void )
  54. {
  55.   return (bool) feof(stream);
  56. }
  57.  
  58. class ostream : public ios {
  59. public:
  60.   ostream(FILE* f) : ios(f) { the_base = dec; the_width = 0; }
  61.   ~ostream() { fclose( stdout ); }
  62.   ostream& operator << (  Selector );
  63.   ostream& operator << (  char );
  64.   ostream& operator << (  int );
  65.   ostream& operator << (  char * );
  66.   ostream& operator >> (  char& );
  67.   bool eof( void );
  68. protected:
  69.   void write_the_base( int number, int the_base, int the_width);
  70.   void prints( char *str, int the_width );
  71.   int strlen( char *str );
  72. private:
  73.   Selector the_base;
  74.   int the_width;
  75. };
  76.  
  77. ostream& ostream::operator << ( char c )
  78. {
  79.   putchar( c );
  80.   return *this;
  81. }
  82.  
  83. ostream& ostream::operator << ( int i )
  84. {
  85.   switch ( the_base )
  86.   {
  87.     case dec: write_the_base( i , 10, the_width ); break;
  88.     case oct: write_the_base( i , 8 , the_width ); break;
  89.     case hex: write_the_base( i , 16, the_width ); break;
  90.   }
  91.   return *this;
  92. }
  93.  
  94. ostream& ostream::operator << ( char *str )
  95. {
  96.   while ( *str ) putchar ( *str++ );
  97.   return *this;
  98. }
  99.  
  100.  
  101. ostream& ostream::operator << ( ios::Selector sel )
  102. {
  103.   the_base = sel;
  104.   return *this;
  105. }
  106.  
  107.  
  108. void ostream::write_the_base( int number, int the_base, int the_width)
  109. {
  110.   bool negative = false;
  111.   if ( number < 0 )
  112.   {
  113.     number = -number; --the_width; negative = true;
  114.   }
  115.   if ( number >= the_base )
  116.   {
  117.     write_the_base( number / the_base , the_base , --the_width );
  118.     number = number % the_base;
  119.   } else {
  120.     int i;
  121.     for( i=1; i<the_width; i++ ) putchar(' ');
  122.     if (negative == true)
  123.     {
  124.       putchar('-'); negative = false;
  125.     }
  126.   }
  127.   putchar( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[number] );
  128. }
  129.  
  130. void ostream::prints( char *str, int the_width )
  131. {
  132.   int l = strlen( str );
  133.   int i;
  134.   for( i=1; i<=the_width-l; i++ ) putchar(' ');
  135.   while ( *str )
  136.   {
  137.     putchar( *str++ );
  138.   }
  139. }
  140.  
  141. int ostream::strlen( char *str )
  142. {
  143.   int i=0;
  144.   while( *str++ ) i++;
  145.   return i;
  146. }
  147.  
  148. class istream : public ios {
  149. public:
  150.   istream(FILE *f) : ios(f) { }
  151.   ~istream() { }
  152.   istream& operator >> ( char& );
  153.   istream& operator >> ( int& );
  154.   istream& operator >> ( char [] );
  155. };
  156.  
  157. istream& istream::operator >> ( char& c )
  158. {
  159.   c = getchar();
  160.   return *this;
  161. }
  162.  
  163. istream& istream::operator >> ( int& i )
  164. {
  165.   char c = getchar();
  166.   int  num = 0;
  167.   while ( ! isdigit(c) ) c = getchar();
  168.   while ( isdigit(c) ) {
  169.     num = num * 10 + (c-'0');
  170.     c = getchar();
  171.   }
  172.   ungetch(c); i = num;
  173.   return *this;
  174. }
  175.  
  176.  
  177. ostream cout(stdout), cerr(stderr);
  178. istream cin(stdin);
  179.  
  180. #endif
  181. /*
  182. void main()
  183. {
  184.   int countdown=10;
  185.   cout << "Decimal" << "\t" << "Octal" << "\t" << "Hex" << "\n";
  186.   while ( countdown > 0 )
  187.   {
  188.     cout << ios::dec << countdown << "\t";
  189.     cout << ios::hex << countdown << "\t";
  190.     cout << ios::oct << countdown << "\t";
  191.     cout << "\n";
  192.     if ( countdown == 3 )
  193.     {
  194.       cout << "Ignition" << "\n";
  195.     }
  196.     countdown--;
  197.   }
  198.   cout << "Blast Off" << "\n";
  199. }
  200. */
  201.